

package cn.shengchao.examstar.pay.model.entity;

import cn.shengchao.examstar.pay.model.valobj.OrderStatusVO;
import com.alibaba.fastjson2.JSON;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 订单实体
 */
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class OrderEntity {

    /**
     * 商品ID
     */
    private String productId;
    // COURSE,PRODUCT_MEMBER,COURSE_NODES
    private String productType;
    private String payType;
    /**
     * 商品
     */
    private String productName;
    /**
     * 订单编号
     */
    private String orderId;
    /**
     * 下单时间
     */
    private Date orderTime;
    /**
     * 订单状态；create-创建完成、pay_wait-等待支付、pay_success-支付成功、deal_done-交易完成、close-订单关单
     */
    private OrderStatusVO orderStatus;
    /**
     * 订单金额
     */
    private BigDecimal totalAmount;
    /**
     * 原价
     */
    private BigDecimal price;
    /**
     * 支付信息
     */
    private String payInfo;

    /**
     * 商品信息
     */
    private String productInfo;
    /**
     * 优惠信息
     */
    private String discountInfo;

    /**
     * 子订单
     */
    private List<OrderItemEntity> orderItemEntities;

    public void addAllOrderItemEntities(List<ProductEntity> subProducts) {
        this.orderItemEntities = new ArrayList<>();
        if (subProducts != null) {
            for (ProductEntity subProduct : subProducts) {
                OrderItemEntity orderItemEntity = OrderItemEntity.builder()
                        .productId(subProduct.getProductId())
                        .productName(subProduct.getProductName())
                        .orderId(this.orderId)
                        .orderTime(this.orderTime)
                        .totalAmount(subProduct.getDiscountPrice())
                        .price(subProduct.getPrice())
                        .productInfo(JSON.toJSONString(subProduct))
                        .build();
                this.orderItemEntities.add(orderItemEntity);
            }
        }
    }
}
